%%******************************************************************************************
%% Replication code, Figure 1: Optimal Trade Policy, Allocations, and Prices under σ = 1 
%% "Technological Rivalry and Optimal Dynamic Policy in an Open Economy"
%% by Bai, Jin, and Lu, May 2025
%%******************************************************************************************

close all

rname = 'Ramsey_sH1_SF1.mat';

ccr = 'b';
lwd = 2;
smc =20;

ititle = 1; % 1 plot title
fposition =[100,100,1300,800];

xx1= 2;% period 1 is steady state
xx2 =100;
iset_yrange =1; % 1 set y-range


%% Ramsey results
load([rname]);

ncountries=2;
nsectors =1;
n = ncountries;
ns = nsectors;
inorm = 200; % normalization according to the second point


vname={'T1','T2','taux','x1','x2','w1','w2','P2','pi11','pi22','rmul','MULT_4','MULT_3','r1','r2'};

v1 = {'theta1'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
theta1 = M_.params(ttt);
theta = theta1;


v1 = {'sigmaH'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
sigmaH = M_.params(ttt);

v1 = {'sigmaF'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
sigmaF = M_.params(ttt);

v1 = {'betad'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
beta = M_.params(ttt);



nv = length(vname);
for i=1:nv
    mm=strcmpi(vname(i),M_.endo_names);
    ttt=find(mm==1);
    if (isempty(ttt)==0)
        ivname(i) = ttt; % declaration order
    else
        ivname(i) =0;

        fprintf(1,'cannot find variable %s\n',char(vname(i)));
    end
    eval(['ri',char(vname(i)),'=',num2str(i),';'])
end

for i=1:nv
    yvar(i,:) = oo_.endo_simul(ivname(i),:);
end

ryvar=yvar;

Tperiod = 1:length(yvar);
ccr2 = 'r';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure 1(a): T1, Ramsey
irow = 3;
icol = 4;

figure(1)
header = 'Figure 1: Optimal Trade Policy, Allocations, and Prices under σ = 1';
findex = 1;
F1 = figure(findex);
set(F1,'numbertitle','off')
set(F1,'name',header)

subplot(irow,icol,1)

yy = 100*(ryvar(riT1,2:end)-ryvar(riT1,inorm));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd,'LineStyle','-')
set(gca,'FontSize',smc)
ylabel('percentage deviation')

xlim([xx1,xx2])
if (iset_yrange==1)
    ylim([-60,0])
end
yticks([-60:10:0])
xticks([0:20:100])
box on
if ititle==1
    title('(a) T1')
end

%% Figure 1(b): taux
subplot(irow,icol,2)

yy = 100*(ryvar(ritaux,2:end)-ryvar(ritaux,inorm));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd,'LineStyle','-')
set(gca,'FontSize',smc)
ylabel('percent')

xlim([xx1,xx2])
if (iset_yrange==1)
    ylim([-6,0])
end
yticks([-6:1:0])
xticks([0:20:100])
box on
if ititle==1
    title('(b) \tau^x')
end
box on

%% Figure 1(c): innovation labor
subplot(irow,icol,3)

yy = 100*(ryvar(rir1,2:end)-ryvar(rir1,inorm));
ylabel('percentage deviation')


plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
xlim([xx1,xx2])
if (iset_yrange==1)
    ylim([0,140])
end

ylabel('percentage deviation')
yticks([0:20:140])
xticks([0:20:100])
box on
set(gca,'Fontsize',smc)
if ititle==1
    title('(c) L_1^r')
end

%% Figure 1(d):  Home consumption
subplot(irow,icol,5)


yy = 100*(ryvar(rix1,:)-ryvar(rix1,inorm));

plot(Tperiod(2:end),yy(2:end),'Color',ccr,'LineWidth',lwd)
set(gca,'Fontsize',smc)
xlim([xx1,xx2])
if (iset_yrange==1)
    ylim([-50,0])
end
ylabel('percentage deviation')
yticks([-50:10:0])
xticks([0:20:100])
box on
if ititle==1
    title('(d) C_1')
end


%% Figure 1(e):  Home wage

subplot(irow,icol,6)

rw1 = exp(ryvar(riw1,:));

yy = 100*(log(rw1)-log(rw1(inorm)));
ylabel('percentage deviation')


plot(Tperiod(2:end),yy(2:end),'Color',ccr,'LineWidth',lwd)
set(gca,'Fontsize',smc)
xlim([xx1,xx2])
if (iset_yrange==1)
    ylim([-25,0])
end
 ylabel('percentage deviation')
yticks([-25:5:0])
xticks([0:20:100])
box on
if ititle==1
    title('(e) w_1/P_1')
end



%% Figure 1(f):  Home interest rate 

subplot(irow,icol,7)

c1 = exp(ryvar(rix1,2:end));
Rt1 = (c1(1:end-1)./c1(2:end)).^(-sigmaH)/beta;

yy = 100*(log(Rt1)-log(Rt1(inorm)));

plot(Tperiod(2:end-1),yy,'Color',ccr,'LineWidth',lwd)
ylabel('percentage deviation')

set(gca,'Fontsize',smc)
xlim([xx1,xx2])
box on
if (iset_yrange==1)
    ylim([0,8])
end
yticks([0:2:8])
xticks([0:20:100])
if ititle==1
    title('(f) R_1')
end

%% Figure 1(g):T2

subplot(irow,icol,9)
yy = 100*(ryvar(riT2,2:end)-ryvar(riT2,inorm));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
ylabel('percentage deviation')

xlim([xx1,xx2])
  ylim([-5,5])
if (iset_yrange==1)
    ylim([-5,5])
end
xticks([0:20:100])
box on
if ititle==1
    title('(g) T_2')
end
set(gca,'Fontsize',smc)

%% Figure 1(h):foreign innovation labor
subplot(irow,icol,10)


yy = 100*(ryvar(rir2,2:end)-ryvar(rir2,inorm));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
ylabel('percentage deviation')

xlim([xx1,xx2])
ylim([-5,5])
xticks([0:20:100])
box on
if ititle==1
    title('(h) L_2^r')
end
set(gca,'Fontsize',smc)

%% Figure 1(i):Foreign wage
subplot(irow,icol,11)

rww2 = ryvar(riw2,:)-yvar(riP2,:);

yy = 100*(rww2(2:end)-rww2(inorm));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)

ylabel('percentage deviation')

set(gca,'Fontsize',smc)
xlim([xx1,xx2])
if (iset_yrange==1)
    ylim([-4,0])
end
yticks([-4:1:0])
xticks([0:20:100])
box on
if ititle==1
    title('(i) w_2/P_2')
end

%% Figure 1(j): Foreign Real interest rate

subplot(irow,icol,12)

c2 = exp(ryvar(rix2,2:end))./exp(ryvar(riP2,2:end));
p2 = exp(ryvar(riP2,2:end));
Rt2 = (c2(1:end-1)./c2(2:end)).^(-sigmaF)/beta;

yy = (log(Rt2)-log(Rt2(inorm)))*100;

plot(Tperiod(2:end-1),yy,'Color',ccr,'LineWidth',lwd)
ylabel('percentage deviation')

set(gca,'Fontsize',smc)
xlim([xx1,xx2])
box on
if (iset_yrange==1)
    ylim([0,0.6])
end
yticks([0:0.1:0.6])
xticks([0:20:100])
if ititle==1
    title('(j) R_2')
end

set(F1,'position',fposition)